import { useUserStore } from '@/stores'
import { createRouter, createWebHistory } from '@ionic/vue-router'
import { RouteRecordRaw } from 'vue-router'
import TabsPage from '../views/Home/LayoutContainer.vue'

const routes: Array<RouteRecordRaw> = [
  { path: '/login', name: 'login', component: () => import('@/views/login/loginPage.vue') }, //登录页
  { path: '/register', name: 'register', component: () => import('@/views/login/registerPage.vue') }, //注册页
  {
    path: '/',
    redirect: '/Home/FindActivity'
  },
  {
    path: '/Home/',
    component: TabsPage,
    children: [
      {
        path: '',
        redirect: '/Home/FindActivity'
      },
      {
        path: 'FindActivity',
        component: () => import('@/views/Home/FindActivity.vue')
      },
      {
        path: 'MyActivities',
        component: () => import('@/views/Home/MyActivities.vue')
      },
      {
        path: 'UserCenter',
        component: () => import('@/views/Home/UserCenter.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

router.beforeEach((to) => {
  // 如果没有token, 且访问的是非登录页，拦截到登录，其他情况正常放行
  const useStore = useUserStore()
  if (!useStore.token && to.path !== '/login' && to.path !== '/register') return '/login'
})

export default router
